'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 装备进阶
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var rewardType = require('../../../../consts/rewardType');
var equipConf = require('../../../../config/equipConf');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var PlayerManager = require('../../../../libs/playerManager');
var constantUtil = require('../../../../util/constantUtil');

/**
 * 消耗材料
 * 将装备转成升级后的新装备
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var equip_id = msg.equip_id;

    if (!equip_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var new_equip_type = 0;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'accrued'],
                    cb
                );
            },
            /* jshint maxstatements:100 */
            function (cb) {
                var role_model = player_manager.role;
                var bag_model = player_manager.bag;

                // 判断原装备是否存在
                var equip = bag_model.getEquip(equip_id);
                if (!equip) {
                    return next(null, {
                        code: Code.BAG_ITEM_NOT_EXIST
                    });
                }

                // 取出锻造配置信息
                var forge_conf = equipConf.getAdvConf(equip.getType());
                if (!forge_conf) {
                    return next(null, {
                        code: Code.BAG_ITEM_EQUIP_ADV_CONF_ERROR
                    });
                }

                // 消耗锻造材料
                for (var i = 1; i <= 5; i++) {
                    var prop_key = 'property_' + i.toString();
                    var num_key = 'num_' + i.toString();
                    if (!forge_conf[prop_key] || !forge_conf[num_key]) {
                        continue;
                    }
                    if (!bag_model.subProperty(forge_conf[prop_key], forge_conf[num_key])) {
                        return next(null, {
                            code: Code.BAG_PROPERTY_NOT_ENOUGH
                        });
                    }
                }

                // 扣除金币
                if (!roleService.subGold(player_manager, moduleType.MDL_EQUIP, operationType.OPR_EQUIP_FORGE, forge_conf.cost_gold)) {
                    return next(null, {
                        code: Code.USER_GOLD_NOT_ENOUGH
                    });
                }

                // 修改装备类型
                new_equip_type = forge_conf.advanced_id;
                equip.setType(new_equip_type);

                // 清空进阶标志
                equip.setAdvanced(0);

                // 增加任务统计
                taskService.addUpgradeEquip(player_manager);

                // 保存数据
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    new_equip_type: new_equip_type
                }
            });
        }
    );
};
